/**
 * https://leetcode.cn/problems/koko-eating-bananas/description/
 * 875.爱吃香蕉的珂珂
 * medium 叶佳豪 2024.09.19
 * 二分搜索
 */

class Solution {
public:
    int minEatingSpeed(vector<int>& piles, int h) {
        int max_piles = 0;
        for (int i = 0; i < piles.size(); i++) {
            max_piles = max(max_piles, piles[i]);
        }
        int left = 1, right = max_piles;
        while (left < right) {
            int mid = left + (right - left) / 2;
            if (canFinish(piles, h, mid)) {
                right = mid;
            } else {
                left = mid + 1;
            }
        }
        return left;
    }
    
    bool canFinish(vector<int>& piles, int h, int k) {
        int time = 0;
        for (int i = 0; i < piles.size(); i++) {
            time += ceil((double)piles[i] / k);
        }
        return time <= h;
    }
};